package com.bxx.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bxx.common.lang.Result;
import com.bxx.entity.Note;
import com.bxx.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class NoteController {
    @Autowired
    NoteService noteService;

    @GetMapping("/notes")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "20") Integer pageSize){
        Page page = new Page(currentPage, pageSize);
        IPage pageData = noteService.page(page, new QueryWrapper<Note>().orderByDesc("created"));
        return Result.succ(pageData);
    }

    @GetMapping("/note/{id}")
    public Result detail(@PathVariable(name="id") Long id){
        Note note = noteService.getById(id);
        Assert.notNull(note, "该笔记已被删除");
        return Result.succ(note);
    }
}
